package com.itheima.recommend.util;

import com.itheima.recommend.pojo.Graph_product;
import com.itheima.recommend.pojo.Node;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * 猜你喜欢 通用工具类
 */
public class SmartSearchUtil {
    public static Graph_product graph_product;

    public static Graph_product build() {
        return new Graph_product();
    }

    public static Graph_product build(int size) {
        return new Graph_product(size);
    }

    static {
        if (graph_product == null) {
            graph_product = SmartSearchUtil.build();
        }
    }

    /**
     * 智能查找
     */
    public static ArrayList<String> smart(Graph_product graph_product, int length, String... productName) {
        if (productName.length <= 0) {
            return null;
        }
        //最小堆
        PriorityQueue<Node> minHeap = new PriorityQueue(length - 1, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.count - o2.count;
            }
        });
        int size = 0;
        for (String s : productName) {
            for (Node node : graph_product.adj(s)) {
                //排除
                if (excludeLaunch(graph_product, node.index, productName)) {
                    continue;
                }
                if (size < length) {
                    minHeap.add(node);
                    size++;
                } else {
                    //取出堆中最小node
                    Node peek = minHeap.peek();
                    if (peek.count < node.count) {
                        //弹出最小的
                        minHeap.poll();
                        //放入大的
                        minHeap.add(node);
                    }
                }
            }
        }
        //根据 index 查找对应的商品名
        ArrayList<String> arr = new ArrayList<>();
        for (Node node : minHeap) {
            arr.add(graph_product.index(node.index));
        }
        return arr;

    }

    /**
     * 排除
     */
    private static boolean excludeLaunch(Graph_product graph_product, Integer node, String... productName) {
        String name = graph_product.index(node);
        if (productName != null || productName.length != 0) {
            for (String s : productName) {
                if (name.equals(s)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 添加边
     */
    public static void addEdge(Graph_product graph_product, String product1, String product2) {
        int index1 = graph_product.index(product1);
        int index2 = graph_product.index(product2);
        graph_product.addEdge(index1, index2);
        System.out.println("添加关系(" + product1 + ":" + product2 + ")");

    }

    /**
     * 添加顶点
     */
    public static void addRoot(Graph_product graph_product, String productName) {
        graph_product.addRoot(productName);
    }

    public static void main(String[] args) {
        SmartSearchUtil smartSearchUtil = new SmartSearchUtil();
        Graph_product graph_product = new Graph_product();
        graph_product.addRoot("小米");
        graph_product.addRoot("华为");
        graph_product.addRoot("苹果");
        graph_product.addRoot("我我我");
        smartSearchUtil.addEdge(graph_product, "小米", "华为");
        smartSearchUtil.addEdge(graph_product, "小米", "苹果");
        smartSearchUtil.addEdge(graph_product, "小米", "我我我");
        smartSearchUtil.addEdge(graph_product, "苹果", "华为");
        smartSearchUtil.addEdge(graph_product, "苹果", "华为");

        ArrayList<String> list = smartSearchUtil.smart(graph_product, 2, "华为", "我我我");
        for (String s : list) {
            System.out.println(s);
        }
    }
}
